<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of OrderdetailController
 *
 * @author Beverley
 */
class Admin_OrderdetailController extends Zend_Controller_Action {
    function indexAction() {
        $orderDetail = new OrderDetailDAO();
        $orderDetailList = $orderDetail->getOrderDetailList();
        $paginator = Zend_Paginator::factory($orderDetailList);
        $page = $this->_getParam('page',1);
        $paginator->setItemCountPerPage(4);
        $paginator->setCurrentPageNumber($page);
        $this->view->paginator = $paginator;
    }

    function updateAction() {
        $order_id = $this->_request->getParam('order_id');
        $product_id = $this->_request->getParam('product_id');

        $pro = new ProductDAO();
        $proRow = $pro->getProduct($product_id);
        $this->view->price = $proRow->price;
        $this->view->name = $proRow->name;
        
        if ($this->_request->isPost()) {
            $quantity = $this->_request->getParam('quantity');

            $product = new Product();
            // for retrevie number of quantity of the product
            $row = $product->fetchRow("id = '$product_id'");
            $proQuantity = $row->quantity;

            $validator = new Zend_Validate_Int();
            if ($validator->isValid($quantity)) {
                $validator = new Zend_Validate_Between(0, $proQuantity);
                // check quantity input is appropriate
                if ($validator->isValid($quantity)) {
                    $price = $quantity * $proRow->price;
                    $array = array(
                        'order_id' => $order_id,
                        'product_id' => $product_id,
                        'quantity' => $quantity,
                        'price' => $price,
                        'status' => Constant::$STATUS_AVAILABLE
                    );
                    $orderDetail = new OrderDetailDAO();
                    $getOrderDetail = $orderDetail->getOrderDetail($order_id, $product_id);
                    $oldPrice = $getOrderDetail->price;
                    $orderDetail->editOrderDetail($array);

                    //also, edit order
                    $order = new OrderDAO();
                    $getOrder = $order->getOrder($order_id);
                    $total = $getOrder->total - $oldPrice + $price;
                    $data = array(
                        'id'=>$order_id, 'total'=>$total);
                    $order->editOrder($data);
                    $mess['success'] = "Update successfully.";
                } else {
                    if ($quantity > $proQuantity)
                        $mess['great'] = "Quantity input is too large.";
                    if ($quantity < 0)
                        $mess['less'] = "Invalid quatity input.";
                }
            } else {
                $mess['digit'] = "Invalid quatity input.";
            }
            if (isset($mess))
                $this->view->status = $mess;
        }
    }

    function deleteAction() {
        $id = $this->_request->getParam('id');
        $product_id = $this->_request->getParam('product_id');
        $orderDetail = new OrderDetailDAO();
        $orderDetail->deleteOrderDetail($id,$product_id);
        $this->_redirect('admin/orderdetail');
    }
}
?>
